//
//  CCTabBarController.swift
//  SwiftStudy
//
//  Created by chang on 2018/1/10.
//  Copyright © 2018年 chang. All rights reserved.
//

import UIKit

class CCTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //背景半透明
        self.tabBar.isTranslucent = true
        //tabBar背景
        self.tabBar.barTintColor = UIColorFromRGB(rgbValue: 0xF6F6F6)
        
        //the font config
        let attributesNormal =  [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 12),NSAttributedStringKey.foregroundColor: UIColorFromRGB(rgbValue: 0x2A2A37)]
        UITabBarItem.appearance().setTitleTextAttributes(attributesNormal, for: UIControlState.normal)
        
        let attributesSelected =  [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 12),NSAttributedStringKey.foregroundColor: UIColor.defaultRedColor]
        UITabBarItem.appearance().setTitleTextAttributes(attributesSelected, for: UIControlState.selected)
        
        //add view controller
        self.addTabBarViewController()
    }
    
    
    func addTabBarViewController() {
        
        let array : NSArray = NSArray.init(array: [["title":"云天河",
                                                    "image":"One_TabBar",
                                                    "selectImage":"One_TabBar_Select",
                                                    "vc":"OneHomeViewController"],
                                                   ["title":"韩菱纱",
                                                    "image":"Two_TabBar",
                                                    "selectImage":"Two_TabBar_Select",
                                                    "vc":"TwoHomeViewController"],
                                                   ["title":"柳梦璃",
                                                    "image":"Three_TabBar",
                                                    "selectImage":"Three_TabBar_Select",
                                                    "vc":"ThreeHomeViewController"],
                                                   ["title":"慕容紫英",
                                                    "image":"Four_TabBar",
                                                    "selectImage":"Four_TabBar_Select",
                                                    "vc":"FourHomeViewController"]])
        let tabArray: NSMutableArray = NSMutableArray.init()
        for (_,value) in array.enumerated() {
            
            let infoDic : NSDictionary = value as! NSDictionary
            let vcStr : String = infoDic.object(forKey: "vc") as! String
            let titleStr : String = infoDic.object(forKey: "title") as! String
            let imageStr : String = infoDic.object(forKey: "image") as! String
            let selectImageStr : String = infoDic.object(forKey: "selectImage") as! String
            
            let vc = self.swiftClassFromString(className: vcStr)
            vc?.title = titleStr
            let nav = UINavigationController(rootViewController:vc!)
            nav.tabBarItem.image = UIImage (named:imageStr)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
            nav.tabBarItem.selectedImage = UIImage (named:selectImageStr)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
            nav.tabBarItem.imageInsets = UIEdgeInsets.init(top: -12, left: 0, bottom: 0, right: 0)
            
            tabArray.add(nav)
        }
        self.viewControllers = (tabArray as! [UIViewController])
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
